<template>
  <div class="header">
    <a href="/" style="padding-left: 30px">
      <img class="logo" src="@/assets/mylogo.png" />
      <span class="title">小红的第一个项目</span>
    </a>
    <el-dropdown @command="handleCommand">
      <span class="el-dropdown-link">
        欢迎使用,{{user.name}}<i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command="a">修改密码</el-dropdown-item>
        <el-dropdown-item command="b">退出系统</el-dropdown-item>
        <!-- <el-dropdown-item command="c">螺蛳粉</el-dropdown-item> -->
      </el-dropdown-menu>
    </el-dropdown>
        <!-- 修改密码对话框 -->
    <el-dialog title="修改密码" :visible.sync="dialogPasswordVisible" width="400px">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" style="margin-top:-30px;margin-bottom:-20px;">
        <el-form-item label="旧密码" prop="oldpass">
          <el-input type="password" v-model="ruleForm.oldpass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newpass">
          <el-input type="password" v-model="ruleForm.newpass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkpass">
          <el-input type="password" v-model="ruleForm.checkpass"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
      </el-dialog>
  </div>
</template>
<script>
import { logout } from "@/api/login";
import passwordApi from "@/api/password";
export default {
  data(){
    const validateoldpass=(rule,value,callback)=>{
      passwordApi.checkPwd(this.user.id,value).then(response=>{
        const resp=response.data
        if(resp.flag){
          callback()
        }
        else{
          callback(new Erroe(resp.message))
        }
      })
    }
      const validatecheckpass=(rule,value,callback)=>{
      if(value!==this.ruleForm.newpass){
        callback(new Error("两次输入密码不一致"))
      }else{
        callback()
      }
    }
    return{
      user:this.$store.state.user.user,
      ruleForm:{
        oldpass:'',
        newpass:'',
        checkpass:''
      },
      rules:{
          oldpass: [
            { required: true, message: "旧密码不能为空", tirgger: "blur" },
            { validator: validateoldpass, trigger: 'blur' }
          ],
          newpass: [
            { required: true, message: "新密码不能为空", tirgger: "blur" }
          ],
          checkpass: [
            { required: true, message: "确认密码不能为空", tirgger: "blur" },
            { validator: validatecheckpass, trigger: ['blur','change'] }  

          ]
      },
      dialogPasswordVisible:false
    }
  },
  methods: {
    LogoutSystem(){
      /* const token = localStorage.getItem("xh-firstmanager-token")
          logout(token).then(response=>{
            const resp=response.data
            if(resp.flag){
              localStorage.removeItem("xh-firstmanager-token")
              localStorage.removeItem("xh-firstmanager-user")
              this.$router.push('./login')
            }
            else{
              this.$message({
                message:resp.message,
                type:'warning',
                duratio:1000
              })
            }
          }) */
          this.$store.dispatch('Logout').then(response=>{
            if(response.flag){
              this.$router.push('/login')
            }else{
              this.$message({
                message:response.message,
                type:'warning',
                duration:2000
              })
            }
          })
    },
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            /* alert('submit!'); */
            passwordApi.updatePwd(this.user.id,this.ruleForm.newpass).then(response=>{
              const resp=response.data
              this.$message({
                message:resp.message,
                type:resp.flag?"success":"warning"
              })
              if(resp.flag){
                this.LogoutSystem()
                this.dialogPasswordVisible=false
              }
              else{}
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
    },
    uodatePwd(){
      this.dialogPasswordVisible=true
      this.$nextTick(()=>{
        this.$refs['ruleForm'].resetFields()
      })
    },
    handleCommand(command) {
      // this.$message("click on item " + command);
      switch (command) {
        case "a":
          /* this.$message("单击了修改密码"); */
          this.uodatePwd()
          break;
        case "b":
          // this.$message("单击了退出系统");
          this.LogoutSystem()
          break;
      }
    }
  }
};
</script>
<style scoped>
.logo {
  vertical-align: middle;
  border-radius: 5px;
}
.title {
  position: absolute;
  color: #fff;
  padding-left: 5px;
  font-size: 20px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #fff;
  font-size: 20px;
}
.el-icon-arrow-down {
  font-size: 20px;
}
.el-dropdown {
  float: right;
  margin-right: 30px;
}
</style>